package top.mschen.blog.service.register;


import com.netflix.appinfo.InstanceInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.eureka.server.event.*;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * EurekaInstanceCanceledEvent 服务下线事件
 *EurekaInstanceRegisteredEvent 服务注册事件
 *EurekaInstanceRenewedEvent 服务续约事件
 *EurekaRegistryAvailableEvent Eureka注册中心启动事件
 *EurekaServerStartedEvent Eureka Server启动事件
 */
@Component
public class EurekaStateChangeListener {

    private static final Logger LOGGER = LoggerFactory.getLogger(EurekaStateChangeListener.class);
    
    @EventListener
    public void listen(EurekaInstanceCanceledEvent event) {
        long timestamp = event.getTimestamp();
        Date date = new Date(timestamp);
        LOGGER.info(event.getServerId() + "\t" + event.getAppName() + " 服务下线"+date+"  "+event.isReplication());
    }

    @EventListener
    public void listen(EurekaInstanceRegisteredEvent event) {
        InstanceInfo instanceInfo = event.getInstanceInfo();
        LOGGER.info(instanceInfo.getAppName() + "进行注册"+event.getLeaseDuration());
    }

    @EventListener
    public void listen(EurekaInstanceRenewedEvent event) {
        LOGGER.info(event.getServerId() + "\t" + event.getAppName() + " 服务进行续约");
    }

    @EventListener
    public void listen(EurekaRegistryAvailableEvent event) {
        long timestamp = event.getTimestamp();
        Date date = new Date(timestamp);
        LOGGER.info("注册中心 启动"+date);
    }

    @EventListener
    public void listen(EurekaServerStartedEvent event) {

        long timestamp = event.getTimestamp();
        Date date = new Date(timestamp);
        LOGGER.info("Eureka Server 启动"+date);
    }
}
